package com.lzl.springboot;

import java.lang.reflect.*;
import java.util.HashMap;

public class A {
    public static void main(String[] args) {
        String name1 = "Lizhenlin";
        Class c1 = name1.getClass();
        System.out.println(c1.getName());

        String name2 = "java.lang.String";
        Class c2=null;
        try {
            c2=Class.forName(name2);
            System.out.println(c2.getName());
            System.out.println(c2.getSuperclass());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Class c11=Boolean.TYPE;
        Class c22=Byte.TYPE;
        Class c33=Float.TYPE;
        Class c44=Double.TYPE;

        Test test=new Test();
//        Class c4=test.getClass();
        Class c4=Test.class;
        Constructor[] constructors;
        constructors=c4.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.print(i+Modifier.toString(constructors[i].getModifiers()) + "参数: ");
            Class[] parameterTypes = constructors[i].getParameterTypes();
            for (int i1 = 0; i1 < parameterTypes.length; i1++) {
                System.out.print(parameterTypes[i1].getName()+" ");
            }
            System.out.println("");
        }
        Constructor[] constructors1;
        constructors1=c4.getDeclaredConstructors();
        for (int i = 0; i < constructors1.length; i++) {
            System.out.print(i+Modifier.toString(constructors1[i].getModifiers()) + "参数: ");
            Class[] parameterTypes = constructors1[i].getParameterTypes();
            for (int i1 = 0; i1 < parameterTypes.length; i1++) {
                System.out.print(parameterTypes[i1].getName()+" ");
            }
            System.out.println("");
        }

        try {
            Constructor constructor=c4.getConstructor(int.class,String.class);
            System.out.println(Modifier.toString(constructor.getModifiers()));
            Class[] parameterTypes = constructor.getParameterTypes();
            for (int i1 = 0; i1 < parameterTypes.length; i1++) {
                System.out.print(parameterTypes[i1].getName()+" ");
            }
            System.out.println("");
            constructor.newInstance(24,"lizhenlin");

            Constructor constructor1=c4.getDeclaredConstructor(String.class);
            constructor1.setAccessible(true);
            constructor1.newInstance("lizhenlin");

            Method method = c4.getDeclaredMethod("welcome", String.class);
            method.setAccessible(true);
            method.invoke(test,"123456");
            method.invoke(c4.getConstructor().newInstance(),"1234567");

            Field field = c4.getDeclaredField("name");
            field.setAccessible(true);
            Object o=c4.getConstructor().newInstance();
            field.set(o,"嘿嘿嘿");
            System.out.println(field.get(o));
        } catch (NoSuchMethodException  e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }


    }
}
